﻿using System;
using System.ComponentModel;
using System.Data.OleDb;
using System.Drawing;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using pingstat;
using System.Net.Sockets;
using System.Net;


namespace WindowsFormsApplication1
{
    public partial class PingStatistic : Form
    {
        public PingStatistic()
        {
            InitializeComponent();
        }

        public struct PingRetVal
        {
            public int pMin, pMax, pAvg, pCnt;
            public PingRetVal (int p1, int p2, int p3, int p4)
            {
                pMin = p1;
                pMax = p2;
                pAvg = p3;
                pCnt = p4;
            }
        }

        public int pbmax = 50;
        public string[,] cfg = new string[20,2];

        public OleDbConnection pingCon = new OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=pingdb.mdb");
        public OleDbCommand pingCmd;
                
        private int pingonce(string server)
        {
            Ping pingSender = new Ping();
            PingOptions options = new PingOptions();
            options.DontFragment = true;
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 2000;
            PingReply reply;
            if (server == "NONE")
            { 
                reply = pingSender.Send("127.0.0.1", timeout, buffer, options);
            }
            else
            {
                reply = pingSender.Send(server, timeout, buffer, options);
            }
            if ((int)reply.RoundtripTime < 200)
            {
                Thread.Sleep(250-(int)reply.RoundtripTime);
            }
            if (reply.Status == IPStatus.Success)
            {
                return (int)reply.RoundtripTime;
            }
            else
            {
            return -1;
            }
        }

        private int pingtcp(string server)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork,
                         SocketType.Stream,
                         ProtocolType.Tcp);
            bool ConState = true;
            sock.LingerState = new LingerOption(true, 2);
            sock.NoDelay = true;
            char[] delimiterChars = { ':' };
            string[] srv = server.Split(delimiterChars);
            try
            {
                sock.Connect(srv[0], int.Parse(srv[1]));
            }
            catch (SocketException ex)
            {
                ConState = false;
            }
            catch (Exception ex)
            {
                ConState = false;
            }
            finally
            {
                if (sock.Connected)
                {
                    sock.Close();
                    ConState = true;
                }
                else
                { 
                    ConState = false; 
                }
            }
            if (ConState == true)
            {
                Thread.Sleep(250);
                return 1;
            }
            else { return -1; }
        }

        private PingRetVal ping10(string server)
        {
            PingRetVal RetVal = new PingRetVal();
            IPAddress ip;
            RetVal.pMin = 9999;
            int pingsum = 0;
            int pingcur = 0;
            for (int i = 0; i < 10; i++)
            {
                if (IPAddress.TryParse(server, out ip) == true)
                {
                    pingcur = pingonce(server);
                }
                else
                {
                    pingcur = pingtcp(server);
                    if (pingcur == -1) { i = i + 5; }
                }
                if (pingcur >= 0)
                {
                    pingsum += pingcur;
                    if (RetVal.pMin > pingcur) { RetVal.pMin = pingcur; }
                    if (RetVal.pMax < pingcur) { RetVal.pMax = pingcur; }
                    RetVal.pCnt++;
                }
            }
            if (RetVal.pCnt == 0)
            {
                RetVal.pMin = 0;
            }
            else 
            {
                RetVal.pAvg = pingsum / RetVal.pCnt;
            }
            return RetVal;
        }

        private Color pbColor (int count)
        {
            Color myColor;
            if ((10 - count) < 1)
            { myColor = System.Drawing.Color.FromArgb(55, 255, 55); }
            else
            {
                if ((10 - count) < 3)
                { myColor = System.Drawing.Color.FromArgb(55, 55, 255); }
                else
                {
                    if ((10 - count) < 5)
                    { myColor = System.Drawing.Color.FromArgb(255, 255, 55); }
                    else
                    { myColor = System.Drawing.Color.FromArgb(255, 55, 55); }
                }
            }
            return myColor;
        }

        private void dbinsert(PingRetVal RetVal, String server)
        {
            if (server != "NONE")
            {
                pingCmd = new OleDbCommand("INSERT INTO pingdata(pingdate, pingserver, pingmin, pingmax, pingavg, pingcnt) VALUES (@pd, @ps, @pi, @px, @pv, @pc)", pingCon);
                pingCmd.Parameters.Add("@pd", OleDbType.Date).Value = DateTime.Now;
                pingCmd.Parameters.Add("@ps", OleDbType.Char).Value = server;
                pingCmd.Parameters.Add("@pi", OleDbType.Integer).Value = RetVal.pMin;
                pingCmd.Parameters.Add("@px", OleDbType.Integer).Value = RetVal.pMax;
                pingCmd.Parameters.Add("@pv", OleDbType.Integer).Value = RetVal.pAvg;
                pingCmd.Parameters.Add("@pc", OleDbType.Integer).Value = RetVal.pCnt;
                pingCon.Open();
                pingCmd.ExecuteNonQuery();
                pingCon.Close();
            }
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button4.Enabled = false;
            label43.Text = cfg[0, 0];
            label44.Text = cfg[1, 0];
            label45.Text = cfg[2, 0];
            label46.Text = cfg[3, 0];
            label47.Text = cfg[4, 0];
            label48.Text = cfg[5, 0];
            label49.Text = cfg[6, 0];
            label50.Text = cfg[7, 0];
            label51.Text = cfg[8, 0];
            label52.Text = cfg[9, 0];
            label53.Text = cfg[10, 0];
            label54.Text = cfg[11, 0];
            label55.Text = cfg[12, 0];
            label56.Text = cfg[13, 0];
            label59.Text = cfg[14, 0];
            label57.Text = cfg[15, 0];
            label60.Text = cfg[16, 0];
            label58.Text = cfg[17, 0];
            label61.Text = cfg[18, 0];
            label62.Text = cfg[19, 0];
            label11.Text = "0";
            while (label11.Text == "0")
            {
                label1.Text = pbmax.ToString();
                progressBar1.Maximum = pbmax;
                progressBar2.Maximum = pbmax;
                progressBar3.Maximum = pbmax;
                progressBar4.Maximum = pbmax;
                progressBar5.Maximum = pbmax;
                progressBar6.Maximum = pbmax;
                progressBar7.Maximum = pbmax;
                progressBar8.Maximum = pbmax;
                progressBar9.Maximum = pbmax;
                progressBar10.Maximum = pbmax;
                progressBar11.Maximum = pbmax;
                progressBar12.Maximum = pbmax;
                progressBar13.Maximum = pbmax;
                progressBar14.Maximum = pbmax;
                progressBar15.Maximum = pbmax;
                progressBar16.Maximum = pbmax;
                progressBar17.Maximum = pbmax;
                progressBar18.Maximum = pbmax;
                progressBar19.Maximum = pbmax;
                progressBar20.Maximum = pbmax;
                while (bgPingS1.IsBusy == false & cfg[0, 1] != "NONE")
                { bgPingS1.RunWorkerAsync(); }
                while (bgPingS2.IsBusy == false & cfg[1, 1] != "NONE")
                { bgPingS2.RunWorkerAsync(); }
                while (bgPingS3.IsBusy == false & cfg[2, 1] != "NONE")
                { bgPingS3.RunWorkerAsync(); }
                while (bgPingS4.IsBusy == false & cfg[3, 1] != "NONE")
                { bgPingS4.RunWorkerAsync(); }
                while (bgPingS5.IsBusy == false & cfg[4, 1] != "NONE")
                { bgPingS5.RunWorkerAsync(); }
                while (bgPingS6.IsBusy == false & cfg[5, 1] != "NONE")
                { bgPingS6.RunWorkerAsync(); }
                while (bgPingS7.IsBusy == false & cfg[6, 1] != "NONE")
                { bgPingS7.RunWorkerAsync(); }
                while (bgPingS8.IsBusy == false & cfg[7, 1] != "NONE")
                { bgPingS8.RunWorkerAsync(); }
                while (bgPingS9.IsBusy == false & cfg[8, 1] != "NONE")
                { bgPingS9.RunWorkerAsync(); }
                while (bgPingS10.IsBusy == false & cfg[9, 1] != "NONE")
                { bgPingS10.RunWorkerAsync(); }
                while (bgPingS11.IsBusy == false & cfg[10, 1] != "NONE")
                { bgPingS11.RunWorkerAsync(); }
                while (bgPingS12.IsBusy == false & cfg[11, 1] != "NONE")
                { bgPingS12.RunWorkerAsync(); }
                while (bgPingS13.IsBusy == false & cfg[12, 1] != "NONE")
                { bgPingS13.RunWorkerAsync(); }
                while (bgPingS14.IsBusy == false & cfg[13, 1] != "NONE")
                { bgPingS14.RunWorkerAsync(); }
                while (bgPingS15.IsBusy == false & cfg[14, 1] != "NONE")
                { bgPingS15.RunWorkerAsync(); }
                while (bgPingS16.IsBusy == false & cfg[15, 1] != "NONE")
                { bgPingS16.RunWorkerAsync(); }
                while (bgPingS17.IsBusy == false & cfg[16, 1] != "NONE")
                { bgPingS17.RunWorkerAsync(); }
                while (bgPingS18.IsBusy == false & cfg[17, 1] != "NONE")
                { bgPingS18.RunWorkerAsync(); }
                while (bgPingS19.IsBusy == false & cfg[18, 1] != "NONE")
                { bgPingS19.RunWorkerAsync(); }
                while (bgPingS20.IsBusy == false & cfg[19, 1] != "NONE")
                { bgPingS20.RunWorkerAsync(); }

                Application.DoEvents();
                Thread.Sleep(250);
                //textBox1.AppendText("*");
            }
            button1.Enabled = true;
            button4.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label11.Text = "1";
        }

        private void PingStatistic_Load(object sender, EventArgs e)
        {
            this.Text = this.Text + " - " + Application.ProductVersion;
            int i = 0;
            XmlReader pingconfig = new XmlTextReader("config.xml");
            while (pingconfig.Read())
            {
                if (pingconfig.NodeType == System.Xml.XmlNodeType.Element)
                {
                    if (pingconfig.Name == "nm")
                    {
                        pingconfig.Read();
                        cfg[i,0]=pingconfig.Value;
                    }
                    if (pingconfig.Name == "ip")
                    {
                        pingconfig.Read();
                        cfg[i,1]=pingconfig.Value;
                        i++;
                    }
                }
            }
        }

        private void bgPingS1_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[0,1]);
        }

        private void bgPingS1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar1.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar1.Maximum = pbmax;
            progressBar1.Value = RetVal.pAvg;
            panel1.BackColor = pbColor(RetVal.pCnt);
            label2.Text = RetVal.pAvg.ToString();
            label4.Text = RetVal.pMax.ToString();
            label6.Text = RetVal.pMin.ToString();
            label8.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[0, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[0, 1]);
        }

        private void bgPingS2_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[1, 1]);
        }


        private void bgPingS2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar2.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar2.Maximum = pbmax;
            progressBar2.Value = RetVal.pAvg;
            panel2.BackColor = pbColor(RetVal.pCnt);
            label3.Text = RetVal.pAvg.ToString();
            label5.Text = RetVal.pMax.ToString();
            label7.Text = RetVal.pMin.ToString();
            label9.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[1, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[1, 1]);
        }

        private void bgPingS3_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[2, 1]);
        }

        private void bgPingS3_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar3.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar3.Maximum = pbmax;
            progressBar3.Value = RetVal.pAvg;
            panel3.BackColor = pbColor(RetVal.pCnt);
            label10.Text = RetVal.pAvg.ToString();
            label12.Text = RetVal.pMax.ToString();
            label13.Text = RetVal.pMin.ToString();
            label14.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[2, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[2, 1]);
        }

        private void bgPingS4_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[3, 1]);
        }

        private void bgPingS4_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar4.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar4.Maximum = pbmax;
            progressBar4.Value = RetVal.pAvg;
            panel4.BackColor = pbColor(RetVal.pCnt);
            label15.Text = RetVal.pAvg.ToString();
            label16.Text = RetVal.pMax.ToString();
            label17.Text = RetVal.pMin.ToString();
            label18.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[3, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[3, 1]);
        }

        private void bgPingS5_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[4, 1]);
        }

        private void bgPingS5_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar5.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar5.Maximum = pbmax;
            progressBar5.Value = RetVal.pAvg;
            panel5.BackColor = pbColor(RetVal.pCnt);
            label19.Text = RetVal.pAvg.ToString();
            label20.Text = RetVal.pMax.ToString();
            label21.Text = RetVal.pMin.ToString();
            label22.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[4, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[4, 1]);
        }

        private void bgPingS6_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[5, 1]);
        }

        private void bgPingS6_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar6.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar6.Maximum = pbmax;
            progressBar6.Value = RetVal.pAvg;
            panel6.BackColor = pbColor(RetVal.pCnt);
            label23.Text = RetVal.pAvg.ToString();
            label24.Text = RetVal.pMax.ToString();
            label25.Text = RetVal.pMin.ToString();
            label26.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[5, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[5, 1]);
        }

        private void bgPingS7_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[6, 1]);
        }

        private void bgPingS7_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar7.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar7.Maximum = pbmax;
            progressBar7.Value = RetVal.pAvg;
            panel7.BackColor = pbColor(RetVal.pCnt);
            label27.Text = RetVal.pAvg.ToString();
            label28.Text = RetVal.pMax.ToString();
            label29.Text = RetVal.pMin.ToString();
            label30.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[6, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[6, 1]);
        }

        private void bgPingS8_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[7, 1]);
        }

        private void bgPingS8_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar8.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar8.Maximum = pbmax;
            progressBar8.Value = RetVal.pAvg;
            panel8.BackColor = pbColor(RetVal.pCnt);
            label31.Text = RetVal.pAvg.ToString();
            label32.Text = RetVal.pMax.ToString();
            label33.Text = RetVal.pMin.ToString();
            label34.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[7, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[7, 1]);
        }

        private void bgPingS9_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[8, 1]);
        }

        private void bgPingS9_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar9.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar9.Maximum = pbmax;
            progressBar9.Value = RetVal.pAvg;
            panel9.BackColor = pbColor(RetVal.pCnt);
            label35.Text = RetVal.pAvg.ToString();
            label36.Text = RetVal.pMax.ToString();
            label37.Text = RetVal.pMin.ToString();
            label38.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[8, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[8, 1]);
        }

        private void bgPingS10_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[9, 1]);
        }

        private void bgPingS10_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar10.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar10.Maximum = pbmax;
            progressBar10.Value = RetVal.pAvg;
            panel10.BackColor = pbColor(RetVal.pCnt);
            label39.Text = RetVal.pAvg.ToString();
            label40.Text = RetVal.pMax.ToString();
            label41.Text = RetVal.pMin.ToString();
            label42.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[9, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[9, 1]);
        }

        private void bgPingS11_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[10, 1]);
        }

        private void bgPingS11_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar11.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar11.Maximum = pbmax;
            progressBar11.Value = RetVal.pAvg;
            panel11.BackColor = pbColor(RetVal.pCnt);
            label63.Text = RetVal.pAvg.ToString();
            label65.Text = RetVal.pMax.ToString();
            label69.Text = RetVal.pMin.ToString();
            label93.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[10, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[10, 1]);
        }

        private void bgPingS12_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[11, 1]);
        }

        private void bgPingS12_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar12.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar12.Maximum = pbmax;
            progressBar12.Value = RetVal.pAvg;
            panel12.BackColor = pbColor(RetVal.pCnt);
            label64.Text = RetVal.pAvg.ToString();
            label67.Text = RetVal.pMax.ToString();
            label73.Text = RetVal.pMin.ToString();
            label94.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[11, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[11, 1]);
        }

        private void bgPingS13_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[12, 1]);
        }

        private void bgPingS13_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar13.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar13.Maximum = pbmax;
            progressBar13.Value = RetVal.pAvg;
            panel13.BackColor = pbColor(RetVal.pCnt);
            label66.Text = RetVal.pAvg.ToString();
            label71.Text = RetVal.pMax.ToString();
            label77.Text = RetVal.pMin.ToString();
            label95.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[12, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[12, 1]);
        }

        private void bgPingS14_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[13, 1]);
        }

        private void bgPingS14_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar14.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar14.Maximum = pbmax;
            progressBar14.Value = RetVal.pAvg;
            panel14.BackColor = pbColor(RetVal.pCnt);
            label68.Text = RetVal.pAvg.ToString();
            label75.Text = RetVal.pMax.ToString();
            label81.Text = RetVal.pMin.ToString();
            label96.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[13, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[13, 1]);
        }

        private void bgPingS15_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[14, 1]);
        }

        private void bgPingS15_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar15.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar15.Maximum = pbmax;
            progressBar15.Value = RetVal.pAvg;
            panel15.BackColor = pbColor(RetVal.pCnt);
            label70.Text = RetVal.pAvg.ToString();
            label79.Text = RetVal.pMax.ToString();
            label83.Text = RetVal.pMin.ToString();
            label97.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[14, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[14, 1]);
        }

        private void bgPingS16_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[15, 1]);
        }

        private void bgPingS16_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar16.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar16.Maximum = pbmax;
            progressBar16.Value = RetVal.pAvg;
            panel16.BackColor = pbColor(RetVal.pCnt);
            label72.Text = RetVal.pAvg.ToString();
            label82.Text = RetVal.pMax.ToString();
            label85.Text = RetVal.pMin.ToString();
            label98.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[15, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[15, 1]);
        }

        private void bgPingS17_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[16, 1]);
        }

        private void bgPingS17_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar17.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar17.Maximum = pbmax;
            progressBar17.Value = RetVal.pAvg;
            panel17.BackColor = pbColor(RetVal.pCnt);
            label74.Text = RetVal.pAvg.ToString();
            label84.Text = RetVal.pMax.ToString();
            label87.Text = RetVal.pMin.ToString();
            label99.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[16, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[16, 1]);
        }

        private void bgPingS18_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[17, 1]);
        }

        private void bgPingS18_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar18.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar18.Maximum = pbmax;
            progressBar18.Value = RetVal.pAvg;
            panel18.BackColor = pbColor(RetVal.pCnt);
            label76.Text = RetVal.pAvg.ToString();
            label86.Text = RetVal.pMax.ToString();
            label89.Text = RetVal.pMin.ToString();
            label100.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[17, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[17, 1]);
        }

        private void bgPingS19_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[18, 1]);
        }

        private void bgPingS19_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar19.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar19.Maximum = pbmax;
            progressBar19.Value = RetVal.pAvg;
            panel19.BackColor = pbColor(RetVal.pCnt);
            label78.Text = RetVal.pAvg.ToString();
            label88.Text = RetVal.pMax.ToString();
            label91.Text = RetVal.pMin.ToString();
            label101.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[18, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[18, 1]);
        }

        private void bgPingS20_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = ping10(cfg[19, 1]);
        }

        private void bgPingS20_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            PingRetVal RetVal = new PingRetVal();
            RetVal = (PingRetVal)e.Result;
            if (RetVal.pMax > pbmax) { pbmax = RetVal.pMax; }
            progressBar20.ForeColor = System.Drawing.Color.FromArgb((int)((255 * RetVal.pAvg) / pbmax), (255 - (int)((255 * RetVal.pAvg) / pbmax)), 150);
            progressBar20.Maximum = pbmax;
            progressBar20.Value = RetVal.pAvg;
            panel20.BackColor = pbColor(RetVal.pCnt);
            label80.Text = RetVal.pAvg.ToString();
            label90.Text = RetVal.pMax.ToString();
            label92.Text = RetVal.pMin.ToString();
            label102.Text = (10 - RetVal.pCnt).ToString();
            //textBox1.Text += cfg[19, 0] + "-" + RetVal.pAvg + ";";
            dbinsert(RetVal, cfg[19, 1]);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            statistika myform = new statistika();
            myform.Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            editcfg editform = new editcfg();
            editform.Show();
            int i = 0;
            XmlReader pingconfig = new XmlTextReader("config.xml");
            while (pingconfig.Read())
            {
                if (pingconfig.NodeType == System.Xml.XmlNodeType.Element)
                {
                    if (pingconfig.Name == "nm")
                    {
                        pingconfig.Read();
                        cfg[i, 0] = pingconfig.Value;
                    }
                    if (pingconfig.Name == "ip")
                    {
                        pingconfig.Read();
                        cfg[i, 1] = pingconfig.Value;
                        i++;
                    }
                }
            }
 
        }

        private void label1_Click(object sender, EventArgs e)
        {
            pbmax = (int)(pbmax / 2);
        }
   }
}
